import javazoom.jl.player.*;
import javazoom.jl.decoder.*;
import java.io.*;

public class MP3Player {

    //public volatile boolean killThread = false;
    InnerPlayerThread innerThread;

    public Player player;
    private InputStream stream;
    public Boolean paused;
    public MP3File currentFile;
 
    public MP3Player(MP3File chosenFile) 
    {
	currentFile = chosenFile;
	if (chosenFile != null){
	    try
		{
		    stream = chosenFile.getStream();
		}
	    catch(Exception e){}
	}
	else {
	    try
		{
		    stream = new FileInputStream("test.mp3");
		}
	    catch(Exception e){}
	}
	try {
	    player = new Player(stream);
	}
	catch(Exception E) {}
	paused = true;
    }

   public MP3Player(InputStream is) 
    {
	if (is != null){
	    try
		{
		    stream = is;
		}
	    catch(Exception e){}
	}
	else {
	    try
		{
		    stream = new FileInputStream("test.mp3");
		}
	    catch(Exception e){}
	}
	try {
	    player = new Player(stream);
	}
	catch(Exception E){}
	paused = true;
    }


    public void play()
    {
	paused = false;
	try {
	    innerThread = new InnerPlayerThread();
	    innerThread.start();
	    while(!player.isComplete())
		{
		    if (this.paused){
			player.playing = false;
		    }
		    else {
			int position = player.getPosition();
			System.out.println("Position: " + position + "lastposition: " + player.lastPosition);
			Thread.sleep(1000);
		    }
		}
	}
	catch(Exception e) {}
	// while(!killThread) {
	// }
	// System.exit(0);
    }


    class InnerPlayerThread extends Thread
    {
    	public void run()
    	{
    	    try
    		{
    		    player.play();
    		}
    	    catch(Exception e){}
    	}
    }
}